﻿using AutoMapper.Internal.Mappers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Himp.DataHub
{
    /// <summary>
    /// 药品目录类别
    /// </summary>
    public class DrugCatalogTypeService : CrudAppService<DrugCatalogType, DrugCatalogTypeDto, Guid, DrugCatalogTypeInput, DrugCatalogTypeCreateOrUpdateDto, DrugCatalogTypeCreateOrUpdateDto>, IDrugCatalogTypeService
    {
        private IRepository<DrugCatalogType, Guid> _drugCatalogTypeRepository;

        public DrugCatalogTypeService(IRepository<DrugCatalogType, Guid> drugCatalogTypeRepository) : base(drugCatalogTypeRepository)
        {
            _drugCatalogTypeRepository = drugCatalogTypeRepository;
        }

        /// <summary>
        /// 获取岗位列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public override async Task<PagedResultDto<DrugCatalogTypeDto>> GetListAsync(DrugCatalogTypeInput input)
        {
            var iQuery = await _drugCatalogTypeRepository.GetQueryableAsync();

            if (input.Pi == 0)
            {
                var itemsDto = ObjectMapper.Map<List<DrugCatalogType>, List<DrugCatalogTypeDto>>(iQuery.ToList());

                return new PagedResultDto<DrugCatalogTypeDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.Pi, input.Ps);
                var itemsDto = ObjectMapper.Map<List<DrugCatalogType>, List<DrugCatalogTypeDto>>(items.ToList());

                return new PagedResultDto<DrugCatalogTypeDto>(totalCount, itemsDto);
            }
        }
    }
}
